import {deteleObject} from "@/sever/utils";

export const gpsConfig = {
    data() {
        return {
            gpsModal:false,//查看gps弹框
            setGpsModal:false,//设置gps弹框
            fenceMap:"",//地图对象
            polygon:"",//地图线对象
            radius:"",//半径
            polygonPointList:[],//多边形
            polygonPointAll:[],//多边形车船所有集合
            address:'',//地图搜索地址
            gpsType:'',//车辆-船舶-所有
        }
    },
    methods: {
        //地图搜索
        searchAddress(){
            if(this.address == "") {
                this.$Modal.warning({
                    title: "您有必填字段",
                    content: "请输入地址"
                });
                return;
            }
            let _that =this;
            AMap.service("AMap.Geocoder", function() {
                let geocoder = new AMap.Geocoder({
                    city: "", //城市设为北京，默认：“全国”
                });
                let marker = new AMap.Marker();
                geocoder.getLocation(_that.address, function(status, result) {
                    if (status === 'complete'&&result.geocodes.length) {
                        let lnglat = result.geocodes[0].location
                        marker.setPosition(lnglat);
                        _that.fenceMap.setFitView(marker);
                    }else{
                        _that.$Message.warning('根据地址查询位置失败');
                        // log.error('根据地址查询位置失败');
                    }
                });
            });

        },
        //查看or编辑电子围栏
        setGps(active,editType){
            this.polygonPointList = [];
            if(active == 1 || active == 2 || active == 3){
                this.getPolygonPointList(active);
                this.gpsModal = true;
                this.fenceMap = new AMap.Map("gps", {
                    zoom: 14,
                    layers: [
                        // 卫星
                        new AMap.TileLayer.Satellite(),
                        // 路网
                        new AMap.TileLayer.RoadNet()
                    ],
                    center: [this.lng, this.lat]
                });
                this.drawArea(-1);
            }else if(active == 4){
                this.gpsType = editType;
                this.getPolygonPointList(editType);
                this.setGpsModal = true;
                this.getGps();
            }
        },
        //匹配多个类型的电子围栏
        getPolygonPointList(active){
            if(this.polygonPointAll.length > 0){
                for (const key in this.polygonPointAll) {
                    let obj = this.polygonPointAll[key];
                    if(obj.code == "fence" && active == 1){
                        if(obj.multiPolygonPointList){
                            this.polygonPointList = obj.multiPolygonPointList;
                        }
                        if(obj.polygonPointList){
                            this.polygonPointList = [obj.polygonPointList];
                        }
                    }else if(obj.code == "car_fence" && active == 2){
                        if(obj.multiPolygonPointList){
                            this.polygonPointList = obj.multiPolygonPointList;
                        }
                        if(obj.polygonPointList){
                            this.polygonPointList = [obj.polygonPointList];
                        }
                    }else if(obj.code == "ship_fence" && active == 3){
                        if(obj.multiPolygonPointList){
                            this.polygonPointList = obj.multiPolygonPointList;
                        }
                        if(obj.polygonPointList){
                            this.polygonPointList = [obj.polygonPointList];
                        }
                    }
                }
            }
        },
        //地图显示
        getGps() {
            let _that = this;
            let overlays = [];
            _that.setGpsModal = true;
            _that.fenceMap = new AMap.Map("container", {
                zoom: 14,
                layers: [
                    // 卫星
                    new AMap.TileLayer.Satellite(),
                    // 路网
                    new AMap.TileLayer.RoadNet()
                ],
                center: [this.lng, this.lat]
            });
            if(this.polygonPointList.length > 0){
                this.drawArea(-1);
            }else{
                this.fenceMap.clearMap();
            }
            let mouseTool = new AMap.MouseTool(_that.fenceMap);
            mouseTool.polygon({
                fillColor:'#00b0ff',
                strokeColor:'#80d8ff'
                //同Polygon的Option设置
            });
            mouseTool.on('draw',function(e){
                overlays.push(e.obj);
                if (overlays.length > 0) {
                    overlays.forEach(item => {
                        let obj = item.Ce || item.De;
                        let pathArr = obj.path;
                        let x= [];
                        //记录多边形的点
                        for ( let i = 0; i < pathArr.length;  i++ ) {
                            let obj = {
                                lng: pathArr[i].lng,
                                lat: pathArr[i].lat,
                            };
                            x.push(obj);
                        }
                        _that.polygonPointList.push(x);
                    })
                    _that.polygonPointList =  deteleObject(_that.polygonPointList);
                }
            })
        },
        //删除点位
        removeGpsRow(index,indexSub){
            this.polygonPointList.forEach((v,i)=>{
                if(i == index){
                    v.splice(indexSub, 1);
                }
            })
        },
        //新增点位
        addGpsRow(index) {
            let obj = {
                lat: "",
                lng: "",
            };
            this.polygonPointList.forEach((v,i) => {
                if(index == i){
                    v.push(obj)
                }
            })
        },
        //新增点位
        delGps(index,list) {
            list.splice(index,1);
            this.polygonPointList = list;
            this.drawArea(-1);
        },
        //画线到地图
        drawArea(index){
            this.fenceMap.clearMap();
            let path = [];
            for (let i = 0; i < this.polygonPointList.length; i++) {
                if(index == i){
                    let arr = this.polygonPointList[i];
                    let pathArr = [];
                    for (let j = 0; j < arr.length; j++) {
                        let obj = arr[j];
                        if (obj.lat == "" || obj.lng == "") {
                            this.$Modal.warning({
                                title: "您有必填字段",
                                content: "经纬度不能为空",
                            });
                            return
                        }
                        let x = [obj.lng,obj.lat];
                        pathArr.push(x)
                    }
                    path.push(pathArr);
                }else{
                    let arr = this.polygonPointList[i];
                    let pathArr = [];
                    for (let j = 0; j < arr.length; j++) {
                        let obj = arr[j];
                        let x = [obj.lng,obj.lat];
                        pathArr.push(x)
                    }
                    path.push(pathArr);
                }
            }
            path.forEach(v => {
                let polygon = new AMap.Polygon({
                    path: v,
                    strokeColor: "#80d8ff",
                    strokeWeight: 6,
                    strokeOpacity: 0.2,
                    fillOpacity: 0.4,
                    fillColor: "#00b0ff",
                    zIndex: 50
                });
                this.fenceMap.add(polygon);
            })
            this.fenceMap.setFitView();
        },
        sumbitGps() {
            for (let i = 0; i < this.polygonPointList.length; i++) {
                if (
                    this.polygonPointList[i].lat == "" ||
                    this.polygonPointList[i].lng == ""
                ) {
                    this.$Modal.warning({
                        title: "您有必填字段",
                        content: "经纬度不能为空",
                    });
                    return;
                }
            }
            this.setGpsModal = false;
        },
    },
    mounted() {
        this.lat = this.$store.state.porjectObject.lat == undefined?30.592335:this.$store.state.porjectObject.lat;
        this.lng = this.$store.state.porjectObject.lng == undefined?114.29484:this.$store.state.porjectObject.lng;
    }
}
